package jiparada;

import javax.swing.*;
import java.awt.*;


public class GSimulatorJiparada {
	static final int DEFAULT_DX = 13;
	static final int DEFAULT_DY = 13;
	WorldJiparada maze;
	GSimPanel gsimpanel;
	
	class GSimPanel extends JPanel {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {		
			int dx = this.getWidth()/maze.getCols();
			int dy = this.getHeight()/maze.getRows();
			int horiz_length = maze.getCols()*dx-1;
			int vert_length = maze.getRows()*dy-1;
			int x = 0, y = 0;			
			// draw horizontal lines
			g.setColor(Color.gray);
			for (int i=0; i < maze.getRows(); ++i) {
				g.drawLine(0, y, horiz_length, y);
				//g.drawLine(0, y, length, y);
				y += dy;
			}
			// draw vertical grid lines
            for (int j = 0; j < maze.getCols(); ++j) {
                g.drawLine(x, 0, x, vert_length);
                x += dx;
            }
            
            y = 0;
            for (int i = 0; i < maze.getRows(); ++i) {
                x = 0;
                for (int j = 0; j < maze.getCols(); ++j) {
                	char c = maze.getMazeData(i, j);
                	switch (c) {
                	case WorldJiparada.OBSTACLE:
                		g.setColor(Color.black);
                		g.fillRect(x, y, dx+1, dy+1);
                		break;
                	case WorldJiparada.PATH:
                		g.setColor(Color.lightGray);
                		g.fillRect(x+1, y+1, dx-1, dy-1);
                		//g.fillRect(x, y, dx, dy);
                		break;
                	case WorldJiparada.GOAL:
                		g.setColor(Color.red);
                		//g.drawString("G", x, y);
                		g.fillOval(x+1, y+1, dx-2, dy-2);
                		break;
                	case WorldJiparada.ROBOT:
                   		g.setColor(Color.blue);
                		g.fillOval(x+1, y+1, dx-2, dy-2);
                		break;
                	case WorldJiparada.EMPTY:
                   		g.setColor(Color.white);
                		g.fillOval(x+1, y+1, dx-2, dy-2);
                		break;                		
                	case WorldJiparada.UNKNOWN:
                   		g.setColor(Color.green);
                		g.fillOval(x+1, y+1, dx-2, dy-2);
                		break;
                	}                    
                    x += dx;
                }
                y += dy;
            }			
		}
	}
	
	public GSimulatorJiparada( WorldJiparada maze) {
		
		this.maze = maze;
	}
	
	void updateGoalPos(int direction)
	{
		maze.updateGoalPos(direction);
		gsimpanel.repaint();
		
	}
	
	void initGui() {
		JFrame frame = new JFrame("EPICROBOTSIM!!1!1");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gsimpanel = new GSimPanel();
		frame.getContentPane().add(gsimpanel);
		frame.setSize(DEFAULT_DX*maze.getCols(), DEFAULT_DY*maze.getRows());
		frame.setVisible(true);
		
	}
	
	boolean isVisited(int direction)
	{
		return maze.isVisited(direction);
	}
	
	public void updateSurroundings(char surroundings[])
	{
		maze.setMazeSorroundings(surroundings);
		gsimpanel.repaint();
		
	}
	
	public void updateRobotPosition(int direction)
	{
		maze.moveRobot(direction);
		gsimpanel.repaint();
	}
	
	PositionJiparada getRobotPosition()
	{
		return maze.getRobotPosition();
	}
	
	char[][] getMapData()
	{
		return maze.getMapData();
	}
	
	PositionJiparada getGoalPos()
	{
		return maze.getGoalPos();
	}
	
	public void setMazeData(char value, int r, int c)
	{
		maze.setMazeData(value, r, c);
		gsimpanel.repaint();
	}
	
	public double go(int maxSteps, int delayMs) {
		initGui();
		int steps;
		double pathLength = 0.0;
		
		
		
		return pathLength;
	}


}
